/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.erossi.pigest.model.soggetto;

import java.util.Calendar;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.XmlWebApplicationContext;

/**
 * 'SoggettoTest' class: classe di test unitario per l'OUT {@link org.erossi.pigest.model.soggetto.Soggetto} 
 *
 * @author erossi
 * @version Sep 9, 2008
 */
public class SoggettoTest extends TestCase {
  
  private static final Integer TEST_CAP = 37020;

  private String timestamp;
  
  private Soggetto soggetto;
  
  private XmlWebApplicationContext apc;

  /**
   * Crea una nuova istanza della classe
   */
  public SoggettoTest() {
    super();
    apc = null;
  }

  /**
   * Prepara l'ambiente per l'esecuzione del test.
   * 
   * @throws java.lang.Exception eccezioni non gestite
   */
  @Before
  public void setUp() throws Exception {
    
    MockServletContext mockServletContext = new MockServletContext(".", new FileSystemResourceLoader());
    MockHttpSession mockHttpSession = new MockHttpSession(mockServletContext);
    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(mockServletContext);
    mockHttpServletRequest.setSession(mockHttpSession);
    
    ServletRequestAttributes sra = new ServletRequestAttributes(mockHttpServletRequest);
    RequestContextHolder.setRequestAttributes(sra);
    
    apc = new XmlWebApplicationContext();
    apc.setServletContext(mockServletContext);
    apc.setConfigLocations(new String[] {"web/WEB-INF/spring-servlet.xml"});
    apc.refresh();

    timestamp = String.format("%1$tM%1$tS%1$tL", Calendar.getInstance());

    soggetto = (Soggetto) apc.getBean("soggetto");
    soggetto.setCognome("COGNOME_" + timestamp);
    soggetto.setNome("NOME_" + timestamp);
    soggetto.setRagioneSociale("RAGSOC_" + timestamp);
    soggetto.setPartitaIVA("PI_" + timestamp);
    soggetto.setCodiceFiscale("CF_" + timestamp);
    soggetto.setIndirizzo("INDIRIZZO_" + timestamp);
    soggetto.setCitta("CITTA_" + timestamp);
    soggetto.setCAP(SoggettoTest.TEST_CAP);
    soggetto.setProvincia("PROV_" + timestamp);
    soggetto.setTelefono("TEL_" + timestamp);
    soggetto.setFax("FAX_" + timestamp);
    soggetto.setEmail("EMAIL_" + timestamp);
    soggetto.setWeb("WEB_" + timestamp);
    
  }
  
  /**
   * Metodo di test per {@link org.erossi.pigest.model.soggetto.Soggetto#equals(Object)}.
   */
  @Test
  public void testEquals() {
    
    Soggetto s1 = (Soggetto) apc.getBean("soggetto");
    Soggetto s2 = (Soggetto) apc.getBean("soggetto");
    assertTrue(s1.equals(s2));
    assertTrue(s2.equals(s1));
    
    s1.setCognome(soggetto.getCognome());
    assertFalse(s1.equals(s2));
    assertFalse(s2.equals(s1));
    s2.setCognome(soggetto.getCognome());
    assertTrue(s1.equals(s2));
    assertTrue(s2.equals(s1));
    
    s1.setNome(soggetto.getNome());
    assertFalse(s1.equals(s2));
    assertFalse(s2.equals(s1));
    s2.setNome(soggetto.getNome());
    assertTrue(s1.equals(s2));
    assertTrue(s2.equals(s1));
    
    s1.setRagioneSociale(soggetto.getRagioneSociale());
    assertFalse(s1.equals(s2));
    assertFalse(s2.equals(s1));
    s2.setRagioneSociale(soggetto.getRagioneSociale());
    assertTrue(s1.equals(s2));
    assertTrue(s2.equals(s1));
    
    s1.setPartitaIVA(soggetto.getPartitaIVA());
    assertFalse(s1.equals(s2));
    assertFalse(s2.equals(s1));
    s2.setPartitaIVA(soggetto.getPartitaIVA());
    assertTrue(s1.equals(s2));
    assertTrue(s2.equals(s1));
    
    s1.setCodiceFiscale(soggetto.getCodiceFiscale());
    assertFalse(s1.equals(s2));
    assertFalse(s2.equals(s1));
    s2.setCodiceFiscale(soggetto.getCodiceFiscale());
    assertTrue(s1.equals(s2));
    assertTrue(s2.equals(s1));
    
    s1.setIndirizzo(soggetto.getIndirizzo());
    assertFalse(s1.equals(s2));
    assertFalse(s2.equals(s1));
    s2.setIndirizzo(soggetto.getIndirizzo());
    assertTrue(s1.equals(s2));
    assertTrue(s2.equals(s1));

    s1.setCitta(soggetto.getCitta());
    assertFalse(s1.equals(s2));
    assertFalse(s2.equals(s1));
    s2.setCitta(soggetto.getCitta());
    assertTrue(s1.equals(s2));
    assertTrue(s2.equals(s1));     
    
    s1.setCAP(soggetto.getCAP());
    assertFalse(s1.equals(s2));
    assertFalse(s2.equals(s1));
    s2.setCAP(soggetto.getCAP());
    assertTrue(s1.equals(s2));
    assertTrue(s2.equals(s1));
   
    s1.setProvincia(soggetto.getProvincia());
    assertFalse(s1.equals(s2));
    assertFalse(s2.equals(s1));
    s2.setProvincia(soggetto.getProvincia());
    assertTrue(s1.equals(s2));
    assertTrue(s2.equals(s1));
  
    s1.setTelefono(soggetto.getTelefono());
    assertFalse(s1.equals(s2));
    assertFalse(s2.equals(s1));
    s2.setTelefono(soggetto.getTelefono());
    assertTrue(s1.equals(s2));
    assertTrue(s2.equals(s1));
    
    s1.setFax(soggetto.getFax());
    assertFalse(s1.equals(s2));
    assertFalse(s2.equals(s1));
    s2.setFax(soggetto.getFax());
    assertTrue(s1.equals(s2));
    assertTrue(s2.equals(s1));
    
    s1.setEmail(soggetto.getEmail());
    assertFalse(s1.equals(s2));
    assertFalse(s2.equals(s1));
    s2.setEmail(soggetto.getEmail());
    assertTrue(s1.equals(s2));
    assertTrue(s2.equals(s1));
    
    s1.setWeb(soggetto.getWeb());
    assertFalse(s1.equals(s2));
    assertFalse(s2.equals(s1));
    s2.setWeb(soggetto.getWeb());
    assertTrue(s1.equals(s2));
    assertTrue(s2.equals(s1));

  }
  
  /**
   * Ripulisce l'ambiente di test dopo l'esecuzione di ogni singolo metodo.
   * 
   * @throws java.lang.Exception eccezioni non gestite
   */
  @After
  public void tearDown() throws Exception {
    apc.close();
  }

}

